home *** CD-ROM | disk | FTP | other *** search
/ InterCD 2000 September / september_2000.iso / intercd / root / ^Linux / cfengine-1.5.3 / contrib / cfdoc.in next >
Encoding:
Text File  |  1997-04-10  |  4.2 KB  |  142 lines

  1. #! @PERL@
  2. # -*- Perl -*-
  3. #
  4. # Copyright (C) 1995 Andrew Ford
  5. #
  6. #    This program is free software; you can redistribute it and/or modify
  7. #    it under the terms of the GNU General Public License as published by
  8. #    the Free Software Foundation; either version 2 of the License, or
  9. #    (at your option) any later version.
  10. #
  11. #    This program is distributed in the hope that it will be useful,
  12. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. #    GNU General Public License for more details.
  15. #
  16. # cfdoc -- Simple utility to document a cfengine configuration file 
  17. #          (or other configuration file that uses '#' in the first 
  18. #          line to indicate comments).
  19. #
  20. # Author:
  21. #   Andrew Ford                     Email:  andrew@icarus.demon.co.uk
  22. #   Independent Software Consultant WWW:    http://www.nhbs.co.uk/aford/aford.html
  23. #   "Brittany", Wells Road,         Tel:    +44 1452 770836  Fax: 770835
  24. #   Eastcombe, Stroud, GL6 7EE, GB  Mobile: +44 385 258278
  25. #
  26. # Comments starting in column 0 are regarded as text to be typeset (this
  27. # can contain arbitrary markup), while other lines are regarded as code 
  28. # to be set in a verbatim environment.  The default comment indicator is
  29. # '#' and verbatim environments are enclosed in \begin{verbatim} and
  30. # \end{verbatim} pairs, but this behaviour can be overridden with command
  31. # line options.
  32.  
  33. &parse_cmd_line;
  34. &format_file;
  35.  
  36. # Format a file
  37.  
  38. sub format_file {
  39.     while (<>) {
  40.  
  41.     # Start state - look for "#!/" on first line and ignore if found.
  42.     
  43.     next if /^\#!(.*)/ && !$state++;
  44.     
  45.     chop if /\n$/;
  46.     
  47.     # Lines that start with a '#' in the first column are printed
  48.     # without the '#'
  49.     # If the previous line was not part of a comment then the
  50.     # currently open verbatim environment is closed.
  51.     
  52.     if (/^$comment_marker\s*(.*)/) {
  53.         if ($in_code) {
  54.         print($end_verbatim);
  55.         $in_code = 0;
  56.         }
  57.         print("$1\n");
  58.         $blank_lines = 0;
  59.  
  60.     } 
  61.     
  62.     # Other lines are printed in a verbatim environment (which
  63.     # is opened if not already open).
  64.     # Blank lines are counted and only output if they apear within
  65.     # a block of code.
  66.  
  67.     else {
  68.         $blank_lines++, next if /^\s*$/;    
  69.         if (!$in_code) {
  70.         print($start_verbatim);
  71.         $in_code = 1;
  72.         } elsif ($blank_lines) {
  73.         foreach $i (1 .. $blank_lines) {
  74.             print("\n");
  75.         }
  76.         $blank_lines = 0;
  77.         }
  78.         print("$_\n");
  79.     }
  80.     }
  81.  
  82.     print $end_verbatim if $in_code;
  83. }
  84.  
  85.  
  86. # Parse the command line
  87.  
  88. sub parse_cmd_line {
  89.  
  90.     $comment_marker         = "#";
  91.     $latex_start_verbatim   = "\\begin{verbatim}\n";
  92.     $latex_end_verbatim     = "\\end{verbatim}\n";
  93.     $html_start_verbatim    = "<PRE>\n";
  94.     $html_end_verbatim      = "</PRE>\n";
  95.     $texinfo_start_verbatim = "\@smallexample\n";
  96.     $texinfo_end_verbatim   = "\@end smallexample\n";
  97.  
  98.     $start_verbatim = $latex_start_verbatim;
  99.     $end_verbatim   = $latex_end_verbatim;
  100.  
  101.     $usage = "usage: $0 [options] <file\n" .
  102.           "    -c comment_marker     string indicating comment line (default is '#')\n" .
  103.          "    -l language           markup language to use (default is LaTeX)\n" .
  104.          "    -s start-marker       markup for start of code (default \\begin{verbatim})\n" .
  105.          "    -e end-marker         markup for end of code (default \\end{verbatim})\n" .
  106.          "Known languages are \"LaTeX\" (default), \"HTML\" and \"texinfo\".\n";
  107.  
  108.     require "getopts.pl";
  109.     &Getopts('c:l:s:e:') || die $usage;
  110.  
  111.     if (defined($opt_c)) {
  112.     $comment_marker = $opt_c;
  113.     }
  114.  
  115.     if (defined($opt_l)) {
  116.     if ($opt_l eq "latex" || $opt_l eq "LaTeX") {
  117.         $start_verbatim = $latex_start_verbatim;
  118.         $end_verbatim   = $latex_end_verbatim;
  119.     } elsif ($opt_l eq "html" || $opt_l eq "HTML") {
  120.         $start_verbatim = $html_start_verbatim;
  121.         $end_verbatim   = $html_end_verbatim;
  122.     } elsif ($opt_l eq "texinfo" || $opt_l eq "TEXINFO") {    
  123.         $start_verbatim = $texinfo_start_verbatim;
  124.         $end_verbatim   = $texinfo_end_verbatim;
  125.     } else {
  126.         die "Unknown markup language: \"$opt_l\".\n" . $usage;
  127.     }
  128.     }
  129.     if (defined($opt_s)) {
  130.     $start_verbatim = $opt_s;
  131.     }
  132.     if (defined($opt_e)) {
  133.     $end_verbatim = $opt_e;
  134.     }
  135. }
  136.  
  137.  
  138.  
  139.  
  140.  
  141.